﻿using GalaSoft.MvvmLight.Ioc;
using HRM.Model;
using System;
using System.Collections.Generic;

namespace HRM.ViewModel
{
    public class CertificateViewModel : ViewModelGeneric<ChungChi>
    {
        private IEntityService<DMChungChi> _categoryCertificateService;

        public CertificateViewModel(IEntityService<ChungChi> service, IEntityService<DMChungChi> categoryCertificateService)
            : base(service)
        {
            _categoryCertificateService = categoryCertificateService;
            CategoryCertificates = _categoryCertificateService.GetAll();
            if (CategoryCertificates.Count > 0) CategoryCertificateSelected = CategoryCertificates[0];
            if (SimpleIoc.Default.IsRegistered<NhanVien>()) Staff = SimpleIoc.Default.GetInstance<NhanVien>();
            else
            {
                throw new Exception("Staff not found");
            }
        }

        public NhanVien Staff { get; private set; }

        public string Title
        {
            get { return string.Format("Tham gia dự án - {0}", Staff.HoTen); }
        }

        public List<DMChungChi> CategoryCertificates { get; private set; }

        private DMChungChi _ccSeleclted;

        public DMChungChi CategoryCertificateSelected
        {
            get { return _ccSeleclted; }
            set
            {
                if (_ccSeleclted != value)
                {
                    _ccSeleclted = value;
                    RaisePropertyChanged("CategoryCertificateSelected");
                }
            }
        }

        public override ChungChi SelectedItem
        {
            get
            {
                return base.SelectedItem;
            }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    if (value != null)
                    {
                        CategoryCertificateSelected = CategoryCertificates.Find(x => x.MaDMChungChi == _ccSeleclted.MaDMChungChi);
                    }
                    RaisePropertyChanged("SelectedItem");
                }
            }
        }
    } 
}
